package com.example.administrator.myleb;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.administrator.myleb.Fragment.A02_F01;
import com.example.administrator.myleb.Fragment.A02_F02;
import com.example.administrator.myleb.Fragment.A02_F03;
public class A02 extends FragmentActivity implements AdapterView.OnItemClickListener {
    private ListView listView;
    private Fragment f01= new A02_F01();
    private Fragment f02 = new A02_F02();
    private Fragment mFragment;
    private GridView gridView;int i=0;
    private static String[] strs = {"0显/隐","1查找","2修改","3增加","4f01","5f02"};
    private Fragment f03=new A02_F03();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid);
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().add(R.id.fm,f01).commit();
        mFragment = f01;
        initView();}
    private void initView() {
        gridView = (GridView) findViewById(R.id.gridView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.mylistitem,strs);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(this);
    }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        switch (arg2) {
            case 0:
                break;
            case 1:
                switchFragment(f01);
                Log.d("TAG", "onItemClick:-------- "+arg2);
                break;
            case 2:
                switchFragment(f02);
                break;
            case 3:
                switchFragment(f03);
                break;
            case 4:
                Toast.makeText(this,"4", Toast.LENGTH_SHORT).show();
                break;
        }
    }
    private void switchFragment(Fragment fragment) {
        //判断当前显示的Fragment是不是切换的Fragment
        if(mFragment != fragment) {
            //判断切换的Fragment是否已经添加过
            if (!fragment.isAdded()) {
                //如果没有，则先把当前的Fragment隐藏，把切换的Fragment添加上
                getSupportFragmentManager().beginTransaction().hide(mFragment)
                        .add(R.id.fm,fragment).commit();
            } else {
                //如果已经添加过，则先把当前的Fragment隐藏，把切换的Fragment显示出来
                getSupportFragmentManager().beginTransaction().hide(mFragment).show(fragment).commit();
            }
            mFragment = fragment;
        }
    }

}